require( '$d/css/common.less' );
require( '$d/js/modules/header/style.less' );
require( './style.less' );
define( [
  'jquery',
  'require',
  "message",
  "api"
], function( $, require ) {
  var thisApi = require( "api" );
  var ShowTitleMessage = require( "message" );
  var showTime = ShowTitleMessage.showTime;

  //验证码验证
  var codeRe = /^[0-9]{6}$/;
  var emailRe = /^[0-9]{4}[a-zA-Z]{2}$/;

  $( function() {
    //验证码刷新
    $( '.verify_img img' ).click( function() {
      $( ".verify_img img" ).attr( "src", thisApi.FORGET_PASSWORD.CAPTCHA );
    } );
    //账号验证页面下一步按钮
    $( '#account_next' ).click( function() {
      if ( $( '#user_account' ).val() != '' ) {
        $.ajax( {
          cache: true,
          type: "POST", //用POST方式传输
          dataType: "text", //数据格式
          url: thisApi.FORGET_PASSWORD.ACCOUNTVERIFY, //目标地址
          data: {
            user_account: $( '#user_account' ).val()
          },
          error: function( msg ) {
            alert( '验证失败' );
          },
          success: function( msg ) {
            var tmp = JSON.parse( msg );
            msg = tmp.data;
            if ( msg == 'email' ) {
              if ( $( '#verify_code' ).val() != '' ) {
                //判断验证码格式6位
                if ( emailRe.test( $( '#verify_code' ).val() ) ) {
                  //判断验证码
                  $.ajax( {
                    cache: true,
                    type: "POST", //用POST方式传输
                    dataType: 'text',
                    url: thisApi.FORGET_PASSWORD.CHECK_MAIL, //目标地址
                    data: {
                      user_email: $( '#user_account' ).val(),
                      valid_code: $( '#verify_code' ).val()
                    },
                    success: function( msg ) {
                      var tmp = JSON.parse( msg );
                      msg = tmp.msg;
                      if ( msg == 'ok' ) {
                        $( ".content-map > p" ).removeClass( "fontgreen" ).eq( 1 ).addClass( "fontgreen" );
                        $( ".content-map > span" ).removeClass( "backgreen" ).eq( 1 ).addClass( "backgreen" );
                        $( ".login-list  > li" ).eq( 0 ).hide();
                        $( ".login-list  > li" ).eq( 1 ).show();
                        retrievepwd();
                      } else {
                        ShowTitleMessage.showWarning( msg, showTime );
                      }
                    },
                    error: function() {
                      ShowTitleMessage.showWarning( '验证失败', showTime );
                    },
                  } )
                } else {
                  ShowTitleMessage.showWarning( '验证码格式不正确', showTime );
                }
              } else {
                ShowTitleMessage.showWarning( '请输入验证码', showTime );
              }
            } else if ( msg == 'phone' ) {
              if ( $( '#verify_code' ).val() != '' ) {
                //判断验证码格式6位
                if ( codeRe.test( $( '#verify_code' ).val() ) ) {
                  //判断验证码
                  $.ajax( {
                    cache: true,
                    type: "POST", //用POST方式传输
                    dataType: 'text',
                    url: thisApi.FORGET_PASSWORD.VERIFY_SMS, //目标地址
                    data: {
                      user_phone: $( '#user_account' ).val(),
                      valid_code: $( '#verify_code' ).val()
                    },
                    error: function() {
                      ShowTitleMessage.showWarning( '验证失败', showTime );
                    },
                    success: function( msg ) {
                      var data = eval( '(' + msg + ')' );
                      if ( data.ret == 0 ) {
                        $( ".content-map > p" ).removeClass( "fontgreen" ).eq( 1 ).addClass( "fontgreen" );
                        $( ".content-map > span" ).removeClass( "backgreen" ).eq( 1 ).addClass( "backgreen" );
                        $( ".login-list  > li" ).eq( 0 ).hide();
                        $( ".login-list  > li" ).eq( 1 ).show();
                        retrievepwd();
                      } else {
                        ShowTitleMessage.showWarning( data.msg, showTime );
                      }
                    }
                  } )
                } else {
                  ShowTitleMessage.showWarning( '验证码格式不正确', showTime );
                }
              } else {
                ShowTitleMessage.showWarning( '请输入验证码', showTime );
              }
            }
          }
        } );
      } else {
        ShowTitleMessage.showWarning( '请输入账号', showTime );
      }
    } )
    $( '#pwd_next' ).attr( "disabled", "true" );
    $( '#go-bigwe' ).attr( "disabled", "true" );

  } );

  function retrievepwd() {
    $( '#pwd_next' ).removeAttr( "disabled" );
    //设置新密码页面下一步按钮
    $( '#pwd_next' ).click( function() {
      if ( $( '#user_pwd' ).val() != '' ) {
        if ( $( '#user_pwd' ).val().length < 6 ) {
          ShowTitleMessage.showWarning( '密码不能低于6位', showTime );
        } else if ( $( '#user_pwd' ).val().length > 20 ) {
          ShowTitleMessage.showWarning( '密码不能多于20位', showTime );
        } else {
          if ( $( '#user_repwd' ).val() != '' ) {
            if ( $( '#user_repwd' ).val().length < 6 ) {
              ShowTitleMessage.showWarning( '确认密码不能低于6位', showTime );
            } else if ( $( '#user_repwd' ).val().length > 20 ) {
              ShowTitleMessage.showWarning( '确认密码不能多于20位', showTime );

            } else {
              if ( $( '#user_repwd' ).val() == $( '#user_pwd' ).val() ) {
                $.ajax( {
                  cache: true,
                  type: "POST", //用POST方式传输
                  dataType: 'text',
                  url: thisApi.FORGET_PASSWORD.RETRIEVEPWD, //目标地址
                  data: {
                    user_account: $( '#user_account' ).val(),
                    user_pwd: $( '#user_pwd' ).val(),
                    user_repwd: $( '#user_repwd' ).val(),
                    valid_code: $( '#verify_code' ).val()
                  },
                  error: function() {
                    ShowTitleMessage.showWarning( '修改失败', showTime );
                  },
                  success: function( msg ) {
                    var tmp = JSON.parse( msg );
                    msg = tmp.msg;
                    if ( msg == 'ok' ) {
                      $( ".content-map > p" ).removeClass( "fontgreen" ).eq( 2 ).addClass( "fontgreen" );
                      $( ".content-map > span" ).removeClass( "backgreen" ).eq( 2 ).addClass( "backgreen" );
                      $( ".login-list  > li" ).eq( 1 ).hide();
                      $( ".login-list  > li" ).eq( 2 ).show();
                      toBigwe()
                    }
                  }
                } )
              } else {
                ShowTitleMessage.showWarning( '两次输入的密码不一致', showTime );
              }
            }
          } else {
            ShowTitleMessage.showWarning( '请再次输入密码', showTime );
          }
        }

      } else {
        ShowTitleMessage.showWarning( '请输入密码', showTime );
      }
    } )
  }

  //前往微知
  function toBigwe() {
    $( '#go-bigwe' ).removeAttr( "disabled" );
    //前往微知
    $( '#go-bigwe' ).click( function() {
      location.href = 'working_place';
    } )
  }

  //验证码发送
  var InterValObj; //timer变量，控制时间
  var count = 60; //间隔函数，1秒执行
  var curCount; //当前剩余秒数
  function sendMessage() {
    var page_token = $( 'input#page_token' ).val();
    if ( $( '#user_account' ).val() != '' ) {
      //设置button效果，开始计时
      $.ajax( {
        cache: true,
        type: "POST", //用POST方式传输
        dataType: "text", //数据格式
        url: thisApi.FORGET_PASSWORD.ACCOUNTVERIFY, //目标地址
        data: {
          user_account: $( '#user_account' ).val()
        },
        success: function( msg ) {
          //账号类型为邮箱，发送邮件验证
          var tmp = JSON.parse( msg );
          msg = tmp.data;
          if ( msg == 'email' ) {
            $( ".get-code" ).attr( "disabled", "true" );
            $( ".get-code" ).css( 'background', '#ccc' );
            $.ajax( {
              cache: true,
              type: "POST", //用POST方式传输
              dataType: "text", //数据格式
              url: thisApi.FORGET_PASSWORD.SEND_MAIL, //目标地址
              data: {
                user_email: $( '#user_account' ).val()
              },
              error: function( msg ) {
                var tmp = JSON.parse( msg );
                msg = tmp.data;
                ShowTitleMessage.showWarning( '发送失败', showTime );
              },
              success: function( msg ) {
                if ( msg == 'email_over' ) {
                  ShowTitleMessage.showWarning( '今日邮箱验证已用完', showTime );
                  $( ".get-code" ).removeAttr( "disabled" );
                  $( ".get-code" ).css( 'background', '#ffffff' );
                } else if ( msg == 'error_send' ) {
                  curCount = count;
                  $( ".get-code" ).val( curCount + "s后重新获取" );
                  InterValObj = window.setInterval( SetRemainTime, 1000 );
                  ShowTitleMessage.showWarning( '邮件发送失败', showTime );
                } else if ( msg == 'success' ) {
                  curCount = count;
                  $( ".get-code" ).val( curCount + "s后重新获取" );
                  InterValObj = window.setInterval( SetRemainTime, 1000 );
                  ShowTitleMessage.show( '邮件发送成功', showTime );
                } else if ( msg == 'error_email' ) {
                  curCount = count;
                  $( ".get-code" ).text( curCount + "s后重新获取" );
                  InterValObj = window.setInterval( SetRemainTime, 1000 );
                  ShowTitleMessage.showWarning( '邮件发送失败，邮件地址错误', showTime );
                } else if ( msg.indexOf( "time" ) >= 0 ) {
                  curCount = msg.substring( 4 );
                  $( ".get-code" ).val( msg.substring( 4 ) + "s后重新获取" );
                  InterValObj = window.setInterval( SetRemainTime, 1000 );
                  ShowTitleMessage.showWarning( '操作频繁，请稍后在试', showTime );
                  $( ".register > button" ).attr( "disabled", "disabled" );
                } else {
                  curCount = count;
                  $( ".get-code" ).text( curCount + "s后重新获取" );
                  InterValObj = window.setInterval( SetRemainTime, 1000 );
                  ShowTitleMessage.showWarning( '邮件发送失败，邮件地址错误', showTime );
                }
              }
            } );
          }
          //账号类型为手机号，发送短信验证
          else if ( msg == 'phone' ) {
            $( ".get-code" ).attr( "disabled", "true" );
            //$(".get-code").css('background','#ccc');
            $.ajax( {
              type: "POST", //用POST方式传输
              dataType: "json", //数据格式
              url: thisApi.FORGET_PASSWORD.SEND_MESSAGE, //目标地址
              data: {
                phone: $( '#user_account' ).val(),
                //sms_token:page_token,
                verify: $( '#img_verify_code' ).val(),
                page: 'pwd'
              },
              error: function( data ) {
                ShowTitleMessage.showWarning( '发送失败', showTime );
              },
              success: function( data ) {
                switch ( data.ret ) {
                  case 41000:
                    //图片验证码错误ok
                    $( ".get-code" ).removeAttr( "disabled" );
                    ShowTitleMessage.showWarning( data.msg, showTime );
                    $( ".verify_img img" ).attr( "src", thisApi.FORGET_PASSWORD.CAPTCHA );
                    break;
                  case 41001:
                    //请填写图片验证码ok
                    $( '#img_validate' ).modal( 'show' );
                    $( ".get-code" ).removeAttr( "disabled" );
                    //ShowTitleMessage.showWarning('请填写图片验证码', showTime);
                    break;
                  case 41003:
                    //今日短信验证码条数已用完ok
                    $( '#img_validate' ).modal( 'hide' );
                    ShowTitleMessage.showWarning( data.msg, showTime );
                    $( ".get-code" ).removeAttr( "disabled" );
                    $( '#img_verify_code' ).val( '' );
                    break;
                  case 41004:
                    //短信发送失败
                    curCount = count;
                    $( ".get-code" ).val( curCount + "s后重新获取" );
                    InterValObj = window.setInterval( SetRemainTime, 1000 );
                    $( '#img_validate' ).modal( 'hide' );
                    ShowTitleMessage.showWarning( data.msg, showTime );
                    $( '#img_verify_code' ).val( '' );
                    break;
                  case 0:
                    //发送成功
                    curCount = count;
                    $( ".get-code" ).val( curCount + "s后重新获取" );
                    InterValObj = window.setInterval( SetRemainTime, 1000 );
                    $( '#img_validate' ).modal( 'hide' );
                    ShowTitleMessage.show( data.msg, showTime );
                    $( '#img_verify_code' ).val( '' );
                    break;
                  case 41006:
                    //该手机号已被注册
                    ShowTitleMessage.showWarning( data.msg, showTime );
                    $( '#img_validate' ).modal( 'hide' );
                    $( ".get-code" ).removeAttr( "disabled" );
                    $( '#img_verify_code' ).val( '' );
                    break;
                  default:
                    //操作频繁，请稍后再试ok
                    curCount = data.data;
                    $( ".get-code" ).val( msg + "s后重新获取" );
                    InterValObj = window.setInterval( SetRemainTime, 1000 );
                    $( '#img_validate' ).modal( 'hide' );
                    ShowTitleMessage.showWarning( data.msg, showTime );
                    $( ".register > button" ).attr( "disabled", "disabled" );
                    $( '#img_verify_code' ).val( '' );
                    break;
                }
              }
            } );
          }
        }
      } );
    } else {
      ShowTitleMessage.showWarning( '请输入账号', showTime );
    }
  }
  $( '.get-code' ).click( function() {
    sendMessage();
  } );
  $( '#img_validate .btn-success' ).click( function() {
    if ( $( '#img_verify_code' ).val() == '' ) {
      ShowTitleMessage.showWarning( '请填写图片验证码', showTime );
      return;
    }
    sendMessage();
  } );
  $( '#img_validate .btn-default' ).click( function() {
    $( '#img_verify_code' ).val( '' );
  } );
  //timer处理函数
  function SetRemainTime() {
    if ( curCount == 0 ) {
      window.clearInterval( InterValObj ); //停止计时器
      $( ".get-code" ).removeAttr( "disabled" ); //启用按钮
      $( ".get-code" ).css( 'background', 'white' );
      $( ".get-code" ).val( "重新获取验证码" );
    } else {
      curCount--;
      $( ".get-code" ).val( curCount + "s" );
    }
  }
} );
